/**
 *
 */

$(document)
    .ready(
        function() {
            $("form").submit(function() {
                return false;
            });

            var VAL = {
                // 此处不同
                T_FSPBOXCHG : "FSPBOXCHG",
                NORMAL : "0000000",
                DISABLED_ATTR : {
                    "disabled" : true
                },
                ENABLED_ATTR : {
                    "disabled" : false
                }
            };

            var domObj = {
                $retboxATxt : $("#retboxATxt"),
                $retboxBTxt : $("#retboxBTxt"),

                buttons : {
                    $query_btn : $("#query_btn"),
                    $save_btn : $("#save_btn"),
                    $savea_btn : $("#savea_btn"),
                },

                grids : {
                    $retPrdAListGrd : $("#retPrdAListGrd"),
                    $retPrdBListGrd : $("#retPrdBListGrd"),
                },
            };

            function initRetPrdAListGrd() {
                domObj.grids.$retPrdAListGrd
                    .jqGrid({
                        url : "",
                        datatype : "local",
                        mtype : "POST",
                        height : 500,
                        width : 700,
                        autowidth : true,
                        shrinkToFit : true,
                        resizable : true,
						/*	loadonce : true,*/
                        viewrecords : true,
                        // rownumbers : true,
                        rowNum :60,
                        multiselect : true,
                        emptyrecords : true,
                        pager : "retPrdAListPg",
                        colModel : [ {
                            name : 'slot_no',
                            index : 'slot_no',
                            label : '子格位',
                            sortable : false,
                            width : 50
                        }, {
                            name : 'box_id_fk',
                            index : 'box_id_fk',
                            label : '箱子代码',
                            sortable : false,
                            width : 100
                        }, {
                            name : 'prd_seq_id',
                            index : 'prd_seq_id',
                            label : '玻璃代码',
                            sortable : false,
                            width : 100
                        }, {
                            name : 'prd_stat',
                            index : 'prd_stat',
                            label : '玻璃状态',
                            sortable : false,
                            width : 100
                        }, {
                            name : 'prd_qty',
                            index : 'prd_qty',
                            label : '面板计数',
                            sortable : false,
                            width : 100
                        }, {
                            name : 'mdl_id_fk',
                            index : 'mdl_id_fk',
                            label : '产品代码',
                            sortable : false,
                            width : 100
                        }, {
                            name : 'prd_grade',
                            index : 'prd_grade',
                            label : '玻璃等级',
                            sortable : false,
                            width : 100
                        } ],
                        ondblClickRow : function(rowIdA) {
                            // 1.获得选中的行ID
                            var rowDataA, selRowIdB, rowDataB, rowDa;
                            rowDataA = domObj.grids.$retPrdAListGrd
                                .jqGrid("getRowData", rowIdA);
                            if (rowDataA.box_id_fk == "") {
                                showErrorDialog("",
                                    "A卡匣该子格位没有数据，请重新选择有数据的子格位！");
                                // domObj.grids.$retPrdAListGrd
                                // 		.jqGrid("resetSelection");
                                domObj.grids.$retPrdBListGrd
                                    .jqGrid("resetSelection");
                                return false;
                            }
                            // 获得B行的选中的数据
                            selRowIdB = domObj.grids.$retPrdBListGrd
                                .jqGrid("getGridParam",
                                    "selrow");
                            if(!selRowIdB){
                                showErrorDialog("","请选择B卡匣空子格位！");
                                return false;
                            }

                            rowDataB = domObj.grids.$retPrdBListGrd
                                .jqGrid("getRowData", selRowIdB);

                            if (rowDataB.box_id_fk) {
                                showErrorDialog("",
                                    "B卡匣该子格位已有数据，请重新选择空子格位！");
								/*	domObj.grids.$retPrdAListGrd
								 .jqGrid("resetSelection");*/
                                domObj.grids.$retPrdBListGrd.setSelection(selRowIdB,false);
                                return false;
                            }
                            // 给b行附上数据，a行删除选中数据
                            // 赋的数据 子格位/玻璃代码要变 setCell
                            domObj.grids.$retPrdBListGrd.jqGrid(
                                "setRowData", selRowIdB,
                                rowDataA);
                            domObj.grids.$retPrdBListGrd.jqGrid(
                                "setCell", selRowIdB, 1,
                                rowDataB.slot_no);
                            //有点小Bug
							/*domObj.grids.$retPrdBListGrd.jqGrid(
							 "setCell", selRowIdB, 2,
							 domObj.$retboxBTxt.val());*/
                            // 删除y问题 (要保留子格位)除了子格位，其他单元格赋空值
							/*domObj.grids.$retPrdAListGrd.jqGrid("delRowData",rowIdA);*/
                            rowDa = {
                                box_id_fk : "",
                                prd_seq_id : "",
                                prd_stat : "",
                                prd_qty : "",
                                mdl_id_fk : "",
                                prd_grade : ""
                            };
                            domObj.grids.$retPrdAListGrd.jqGrid(
                                "setRowData", rowIdA, rowDa);
                        }
                    })
            }
            ;

            initRetPrdAListGrd();

            function initRetPrdBListGrd() {
                domObj.grids.$retPrdBListGrd
                    .jqGrid({
                        url : "",
                        datatype : "local",
                        mtype : "POST",
                        height : 500,
                        width : 700,
                        autowidth : true,
                        shrinkToFit : true,
                        resizable : true,
						/*loadonce : true,*/
                        viewrecords : true,
                        // rownumbers : true,
                        rowNum : 60,
                        multiselect : true,
                        emptyrecords : true,
                        pager : "retPrdBListPg",
                        colModel : [ {
                            name : 'slot_no',
                            index : 'slot_no',
                            label : '子格位',
                            sortable : false,
                            width : 50
                        }, {
                            name : 'box_id_fk',
                            index : 'box_id_fk',
                            label : '箱子代码',
                            sortable : false,
                            width : 100
                        }, {
                            name : 'prd_seq_id',
                            index : 'prd_seq_id',
                            label : '玻璃代码',
                            sortable : false,
                            width : 100
                        }, {
                            name : 'prd_stat',
                            index : 'prd_stat',
                            label : '玻璃状态',
                            sortable : false,
                            width : 100
                        }, {
                            name : 'prd_qty',
                            index : 'prd_qty',
                            label : '面板计数',
                            sortable : false,
                            width : 100
                        }, {
                            name : 'mdl_id_fk',
                            index : 'mdl_id_fk',
                            label : '产品代码',
                            sortable : false,
                            width : 100
                        }, {
                            name : 'prd_grade',
                            index : 'prd_grade',
                            label : '玻璃等级',
                            sortable : false,
                            width : 100
                        } ],
                        ondblClickRow : function(rowIdB) {
                            // 1.获得选中的行ID
                            var rowDataB, selRowIdA, rowDataA, rowDa;
                            rowDataB = domObj.grids.$retPrdBListGrd
                                .jqGrid("getRowData", rowIdB);
                            if (rowDataB.box_id_fk == "") {
                                showErrorDialog("",
                                    "B卡匣该子格位没有数据，请重新选择有数据的子格位！");
                                domObj.grids.$retPrdAListGrd
                                    .jqGrid("resetSelection");
                                domObj.grids.$retPrdBListGrd
                                    .jqGrid("resetSelection");
                                return false;
                            }
                            // 获得A行的选中的数据
                            selRowIdA = domObj.grids.$retPrdAListGrd
                                .jqGrid("getGridParam",
                                    "selrow");
                            if(!selRowIdA){
                                showErrorDialog("","请选择A卡匣空子格位！");
                                return false;
                            }
                            rowDataA = domObj.grids.$retPrdAListGrd
                                .jqGrid("getRowData", selRowIdA);

                            if (rowDataA.box_id_fk) {
                                showErrorDialog("",
                                    "A卡匣该子格位已有数据，请重新选择空子格位！");
                                domObj.grids.$retPrdAListGrd.setSelection(selRowIdA,false);

								/*	domObj.grids.$retPrdBListGrd
								 .jqGrid("resetSelection");*/
                                return false;
                            }
                            // 给B行附上数据，A行删除选中数据
                            // 赋的数据 子格位/玻璃代码要变 setCell
                            domObj.grids.$retPrdAListGrd.jqGrid(
                                "setRowData", selRowIdA,
                                rowDataB);
                            domObj.grids.$retPrdAListGrd.jqGrid(
                                "setCell", selRowIdA, 1,
                                rowDataA.slot_no);
							/*domObj.grids.$retPrdAListGrd.jqGrid(
							 "setCell", selRowIdA, 2,
							 domObj.$retboxATxt.val());*/
                            // 删除y问题 (要保留子格位)除了子格位，其他单元格赋空值

                            rowDa = {
                                box_id_fk : "",
                                prd_seq_id : "",
                                prd_stat : "",
                                prd_qty : "",
                                mdl_id_fk : "",
                                prd_grade : ""
                            };
                            domObj.grids.$retPrdBListGrd.jqGrid(
                                "setRowData", rowIdB, rowDa);

                        }


                    })
            }
            ;

            initRetPrdBListGrd();

            function isEmptyObject(e) {
                var t;
                for (t in e)
                    return !1;
                return !0
            }

            var buttonFnc = {
                queryFnc1 : function() {
                    var inObj, outObj, tblCnt, boxSetCodeA, boxSetCodeB;
                    boxSetCodeA = domObj.$retboxATxt.val().trim();
                    boxSetCodeB = domObj.$retboxBTxt.val().trim();
                    if (boxSetCodeA == "") {
                        showErrorDialog("", "请输入卡匣A!");
                        return false;
                    }
                    if (boxSetCodeB == "") {
                        showErrorDialog("", "请输入卡匣B!");
                        return false;
                    }
                    if(boxSetCodeA == boxSetCodeB){
                        showErrorDialog("", "卡匣A和卡匣B不能相同，请重新输入!");
                        return false;
                    }

                    inObj = {
                        trx_id : VAL.T_FSPBOXCHG,
                        action_flg : 'Q',
                        box_idA : boxSetCodeA,
                        box_idB : boxSetCodeB,
                        tbl_cnt : 1
                    }, outObj = comTrxSubSendPostJson(inObj);
                    if (outObj.rtn_code === VAL.NORMAL) {
                        setGridInfo(outObj.oaryA, "#retPrdAListGrd",
                            true);
                        setGridInfo(outObj.oaryB, "#retPrdBListGrd",
                            true);
                    }
                },

                queryFncA : function() {
                    var inObj, outObj, tblCnt, boxSetCodeA;
                    boxSetCodeA = domObj.$retboxATxt.val().trim();
                    boxSetCodeB = domObj.$retboxBTxt.val().trim();
                    if (boxSetCodeA == "") {
                        showErrorDialog("", "请输入卡匣A!");
                        return false;
                    }
                    if(boxSetCodeA == boxSetCodeB){
                        showErrorDialog("", "卡匣A和卡匣B不能相同，请重新输入!");
                        return false;
                    }
                    inObj = {
                        trx_id : VAL.T_FSPBOXCHG,
                        action_flg : 'A',
                        box_idA : boxSetCodeA,
                        tbl_cnt : 1
                    }, outObj = comTrxSubSendPostJson(inObj);
                    if (outObj.rtn_code === VAL.NORMAL) {
                        setGridInfo(outObj.oaryA, "#retPrdAListGrd",
                            true);
                    }
                },

                queryFncB : function() {
                    var inObj, outObj, tblCnt, boxSetCodeB;
                    boxSetCodeA = domObj.$retboxATxt.val().trim();
                    boxSetCodeB = domObj.$retboxBTxt.val().trim();
                    if (boxSetCodeB == "") {
                        showErrorDialog("", "请输入卡匣B!");
                        return false;
                    }
                    if(boxSetCodeA == boxSetCodeB){
                        showErrorDialog("", "卡匣A和卡匣B不能相同，请重新输入!");
                        return false;
                    }
                    inObj = {
                        trx_id : VAL.T_FSPBOXCHG,
                        action_flg : 'B',
                        box_idB : boxSetCodeB,
                        tbl_cnt : 1
                    }, outObj = comTrxSubSendPostJson(inObj);
                    if (outObj.rtn_code === VAL.NORMAL) {
                        setGridInfo(outObj.oaryB, "#retPrdBListGrd",
                            true);
                    }
                },

                saveFnc1 : function() {
                    var inObj, outObj, tblCnt, retprdA, retprdB, boxIdA, boxIdB;
                    // A表格所有行数据
                    retprdA = domObj.grids.$retPrdAListGrd.getRowData();
                    retprdB = domObj.grids.$retPrdBListGrd.getRowData();
                    if (isEmptyObject(retprdA)
                        && isEmptyObject(retprdB)) {
                        showErrorDialog("", "列表为空，不能保存");
                        return false;
                    }
                    boxIdA = domObj.$retboxATxt.val();
                    boxIdB = domObj.$retboxBTxt.val();
                    inObj = {
                        trx_id : VAL.T_FSPBOXCHG,
                        action_flg : 'C',
                        iaryA : retprdA,
                        iaryB : retprdB,
                        box_idA : boxIdA,
                        box_idB : boxIdB,
                        tbl_cnt : 1,
                        // remark :,
                    }, outObj = comTrxSubSendPostJson(inObj);
                    if (outObj.rtn_code === VAL.NORMAL) {
                        // nice play
                        buttonFnc.queryFnc1();
                        showSuccessDialog("卡匣交换成功");
                    }
                },

                // 单保存A卡匣内容
                saveFnca : function() {
                    var rowData1, rowData2, ids, rowData;
                    var inObj, outObj, tblCnt, rowData1, rowData2, boxIdA;
                    // 检查A卡匣grid是否选择了两项
                    ids = domObj.grids.$retPrdAListGrd.jqGrid(
                        'getGridParam', 'selarrrow');
                    if (ids.length != 2) {
                        showErrorDialog("", "请选择两块玻璃！");
                        return false;
                    }

                    for (var i = 0; i < ids.length; i++) {
                        rowData = domObj.grids.$retPrdAListGrd.jqGrid(
                            "getRowData", ids[i]);
                        if (rowData.prd_seq_id == ""
                            || rowData.prd_seq_id == null) {
                            showErrorDialog("", "请选择非空的子格位！");
                            return false;
                        }
                    }
                    // A表格所有行数据
                    rowData1 = domObj.grids.$retPrdAListGrd.jqGrid(
                        "getRowData", ids[0]);
                    rowData2 = domObj.grids.$retPrdAListGrd.jqGrid(
                        "getRowData", ids[1]);
                    var retprdA = [];
                    retprdA.push(rowData1);
                    retprdA.push(rowData2);

                    boxIdA = domObj.$retboxATxt.val();
                    inObj = {
                        trx_id : VAL.T_FSPBOXCHG,
                        action_flg : 'S',
                        iaryA : retprdA,
                        box_idA : boxIdA,
                        tbl_cnt : 1,
                    }, outObj = comTrxSubSendPostJson(inObj);
                    if (outObj.rtn_code === VAL.NORMAL) {
                        // nice play
                        buttonFnc.queryFncA();
                        showSuccessDialog("本卡匣玻璃交换成功");
                    }
                },

            };

            domObj.buttons.$query_btn
                .bind("click", buttonFnc.queryFnc1);
            domObj.buttons.$save_btn.bind("click", buttonFnc.saveFnc1);
            domObj.buttons.$savea_btn.bind("click", buttonFnc.saveFnca);


            domObj.$retboxATxt.keydown(function(event) {
                var inObj, outObj, tblCnt, boxSetCodeA,boxSetCodeB;
                boxSetCodeA = domObj.$retboxATxt.val().trim();
                boxSetCodeB = domObj.$retboxBTxt.val().trim();
                if (event.keyCode == 13) {
                    if (boxSetCodeA == "") {
                        showErrorDialog("", "请输入卡匣A!");
                        return false;
                    }
                    if(boxSetCodeA == boxSetCodeB && boxSetCodeA != ""){
                        showErrorDialog("", "卡匣A和卡匣B不能相同，请重新输入!");
                        return false;
                    }
                    inObj = {
                        trx_id : VAL.T_FSPBOXCHG,
                        action_flg : 'A',
                        box_idA : boxSetCodeA,
						/*box_idB :boxSetCodeB,*/
                        tbl_cnt : 1
                    }, outObj = comTrxSubSendPostJson(inObj);
                    if (outObj.rtn_code === VAL.NORMAL) {
                        setGridInfo(outObj.oaryA, "#retPrdAListGrd",
                            true);
                    }
                }
            });

            domObj.$retboxBTxt.keydown(function(event) {
                var inObj, outObj, tblCnt,boxSetCodeA, boxSetCodeB;
                boxSetCodeA = domObj.$retboxATxt.val().trim();
                boxSetCodeB = domObj.$retboxBTxt.val().trim();
                if (event.keyCode == 13) {
                    if (boxSetCodeB == "") {
                        showErrorDialog("", "请输入卡匣B!");
                        return false;
                    }
                    if(boxSetCodeA == boxSetCodeB && boxSetCodeB !=""){
                        showErrorDialog("", "卡匣A和卡匣B不能相同，请重新输入!");
                        return false;
                    }
                    inObj = {
                        trx_id : VAL.T_FSPBOXCHG,
                        action_flg : 'B',
						/*box_idA :boxSetCodeA,*/
                        box_idB : boxSetCodeB,
                        tbl_cnt : 1
                    }, outObj = comTrxSubSendPostJson(inObj);
                    if (outObj.rtn_code === VAL.NORMAL) {
                        setGridInfo(outObj.oaryB, "#retPrdBListGrd",
                            true);
                    }
                }
            });

            function initPage() {
                domObj.$retboxATxt.val("");
                domObj.$retboxBTxt.val("");
                $("#retPrdAListGrd").jqGrid('clearGridData');
                $("#retPrdBListGrd").jqGrid('clearGridData');
            }
            initPage();
            //模态框拖动
            $(".modal-content").draggable();
            //表格自适应
            function resizeFnc(){
                var offsetBottom, divWidth;
                divWidth = $("#divTotal").width();
                offsetBottom =  $(window).height() - $("#divTotal").offset().top;
                $("#divTotal").height(offsetBottom * 0.85);
                $("#retPrdAListGrd").setGridWidth(divWidth * 0.45);
                $("#retPrdAListGrd").setGridHeight(offsetBottom * 0.80 - 51);

                divWidth = $("#divTotal").width();
                offsetBottom =  $(window).height() - $("#divTotal").offset().top;
                $("#divTotal").height(offsetBottom * 0.85);
                $("#retPrdBListGrd").setGridWidth(divWidth * 0.45);
                $("#retPrdBListGrd").setGridHeight(offsetBottom * 0.80 - 51);

            };
            resizeFnc();
            $(window).resize(function() {
                resizeFnc();
            });
        })